package com.ruoyi.wx.controller;

import java.io.IOException;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.config.Global;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.tomcat.jni.FileInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;


import com.ruoyi.wx.domain.TGoods;
import com.ruoyi.wx.service.ITGoodsService;
import org.springframework.web.multipart.MultipartFile;

/**
 * 商品Controller
 *
 * @author ruoyi
 * @date 2020-05-01
 */
@Controller
@RequestMapping("/wx/goods")
public class TGoodsController extends BaseController {
    private String prefix = "wx/goods";

    @Autowired
    private ITGoodsService tGoodsService;

    @RequiresPermissions("wx:goods:view")
    @GetMapping()
    public String goods() {
        return prefix + "/goods";
    }

    /**
     * 查询商品列表
     */
    @RequiresPermissions("wx:goods:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TGoods tGoods) {
        startPage();
        return getDataTable(tGoodsService.list(new QueryWrapper<>(tGoods)));
    }

    /**
     * 导出商品列表
     */
    @RequiresPermissions("wx:goods:export")
    @Log(title = "商品", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TGoods tGoods) {
        List<TGoods> list = tGoodsService.list(new QueryWrapper<>(tGoods));
        ExcelUtil<TGoods> util = new ExcelUtil<>(TGoods.class);
        return util.exportExcel(list, "goods");
    }

    /**
     * 新增商品
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存商品
     */
    @RequiresPermissions("wx:goods:add")
    @Log(title = "商品", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(TGoods tGoods) {
        tGoods.setGddTime(new Date());
        return toAjax(tGoodsService.save(tGoods));
    }

    /**
     * 修改商品
     */
    @GetMapping("/edit/{goodsId}")
    public String edit(@PathVariable("goodsId") String goodsId, ModelMap mmap) {
        TGoods tGoods = tGoodsService.getById(goodsId);
        mmap.put("tGoods", tGoods);
        return prefix + "/edit";
    }

    /**
     * 修改保存商品
     */
    @RequiresPermissions("wx:goods:edit")
    @Log(title = "商品", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TGoods tGoods) {
        tGoods.setUpdTime(new Date());
        return toAjax(tGoodsService.updateById(tGoods));
    }

    /**
     * 删除商品
     */
    @RequiresPermissions("wx:goods:remove")
    @Log(title = "商品", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(tGoodsService.removeByIds(Arrays.asList(Convert.toStrArray(ids))));
    }


//    @PostMapping("/add")
//    @ResponseBody
//    public AjaxResult addSave(@RequestParam("file") MultipartFile file, FileInfo fileInfo) throws IOException
//    {
//        // 上传文件路径
//        String filePath = Global.getUploadPath();
//        // 上传并返回新文件名称
//        String fileName = FileUploadUtils.upload(filePath, file);
//        fileInfo.setFilePath(fileName);
//        return toAjax(fileInfoService.insertFileInfo(fileInfo));
//    }
}
